package com.cqz.services;

import com.cqz.mapper.BookInfoMapper;
import com.cqz.dao.BookInfo;
import com.cqz.dao.BookTag;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service("tagService")
public class TagService {
    @Autowired
    private SqlSession sqlSession;
    @Autowired
    private BookInfoMapper bookInfoMapper;

    /**
     * 获取所有的标签信息并存储为一个String数组
     * @return
     */
    public String[] getTags() {
        List<String> stringList=sqlSession.selectList("selectAllTags");
        Iterator<String> iterator=stringList.iterator();
        String[]res=new String[stringList.size()];
        int i=0;
        while (iterator.hasNext()){
            res[i]=iterator.next();
            i++;
        }
        return res;
    }

    /***
     * 查询标签中包含searchConten的书籍信息
     * @param searchContent 搜索内容
     * @return
     */
    public List<BookInfo> searchBookAndEdit(String searchContent) {
        BookTag bookTag=new BookTag();
        bookTag.setTag(searchContent);
        List<Integer> integers=sqlSession.selectList("selectAllIds",bookTag);
        int size=integers.size();
        Iterator<Integer> integerIterator=integers.iterator();
        List<BookInfo> res=new ArrayList<>();
        while (integerIterator.hasNext()){
            res.add(bookInfoMapper.selectByPrimaryKey(integerIterator.next()));
        }
        return res;
    }

    /**
     * 根据图书bid得到其相关联的标签
     * @param bid
     * @return
     */
    public List<String> getRefTagsByBookID(int bid) {
        List<String> res=sqlSession.selectList("selectAllTagsByBid",bid);
        return res;
    }

    /**
     * 返回所有标签内容中含有tags中的一项的图书id
     * @param tags  用户选择的标签
     * @return
     */
    public List<Integer> getRecommendTags(List<String> tags) {
        List<Integer> res=sqlSession.selectList("selectAllBidByTags",tags);
        return res;
    }
}
